package client.comm.baoding.ui

import android.content.Intent
import android.os.Bundle
import android.view.View
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.main.MyZcAdapter
import client.comm.baoding.api.bean.AssetsRecode
import client.comm.baoding.api.bean.AssetsRecode2
import client.comm.baoding.api.event.EventTake
import client.comm.baoding.api.event.EventUserInfo
import client.comm.baoding.databinding.ActivityMyZcBinding
import client.comm.baoding.dialog.JyInputNumDialog
import client.comm.baoding.ui.vm.ZcjlViewModel
import client.comm.commlib.comm_ui.draw.SpaceItemBottomDecoration
import client.comm.commlib.comm_ui.draw.SpaceItemDecoration
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 我的资产
 */
class MyZcActivity : BaseMiddleActivity<ZcjlViewModel, ActivityMyZcBinding>() {

    var goods_id: String? = null

    override fun getLayout(): Int {
        return R.layout.activity_my_zc
    }

    override fun initViewModel(): ZcjlViewModel = createViewModel()

    val zcAdapter: MyZcAdapter by lazy {
        MyZcAdapter(this, mImgLoader).apply {
            itemClick = {
//                startActivity(Intent(this@MyZcActivity, GoodsDetailTradeActivity::class.java).apply {
//                    putExtra("goodsId", "${it.goods_id}")
//                })

            }
            tihuo = {

                startActivity(Intent(this@MyZcActivity, TradeAddressActivity::class.java).apply {
                    putExtra("goods_id", it.goods_id)
                    putExtra("type", 2)
                })

                /*JyInputNumDialog(this@MyZcActivity, 2).apply {
                    ok = { num ->
                        viewModel.holdTiHuo(it, num)
                        dismiss()
                    }
                }.show()*/

            }

            tuikuan = {
                JyInputNumDialog(this@MyZcActivity, 5).apply {
                    ok = { num ->
                        viewModel.holdTuiKuan(it, num)
                        dismiss()
                    }
                }.show()
            }
        }
    }

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        goods_id = intent.getStringExtra("goodsId")

        binding.emptyLayout.setOnClickListener {
            viewModel.myTradeList()
//            viewModel.pageOne.value = 0
//            viewModel.holdAssets()

        }
        binding.recyclerView.addItemDecoration(SpaceItemBottomDecoration(10))

        binding.recyclerView.adapter = zcAdapter
        binding.recyclerView.onLoadMore = {
            if (zcAdapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
//                viewModel.holdAssets()
                viewModel.myTradeList()
            }
        }

        binding.refreshLayout.setOnRefreshListener {
            zcAdapter.hasMoreState.value = true
            viewModel.myTradeList()
//            viewModel.pageOne.value = 0
//            viewModel.holdAssets()

        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.myTradeList()
//        viewModel.pageOne.value = 0
//        viewModel.holdAssets()

    }

    override fun initData() {
        super.initData()
        viewModel.callAssets2.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    binding.eTotal = it.data?.total

                    val list =
                        it.data?.my_goods_info as MutableList<AssetsRecode2.MyGoodsInfo?>?
                    zcAdapter.submitList(list)

                    /*if (viewModel.pageOne.value == 1) {
                        zcAdapter.submitList(list)
                    } else
                        zcAdapter.appendList(list)*/

                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            zcAdapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (zcAdapter.itemCount == 0) {
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        }
                        zcAdapter.hasMoreState.value = false
                    }
                } else {
                    if (zcAdapter.itemCount == 0) {
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                    }

                }
            }
        })

        viewModel.refundRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast(it.message)
                    viewModel.myTradeList()
                }
            }
        })
    }


    fun goZichanJl() {
        startActivity(Intent(this, ZcJl2Activity::class.java))
    }

    //用户信息修改成功时，回调刷新
    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun update(event: EventTake?) {
        viewModel.myTradeList()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}